package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DBHelper;
//import cn.wolfcode.wolf2w.vo.CatalogVO;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
//@Transactional  暂时先别管事务
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public StrategyCatalog get(String id) {
        return null;
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        return null;
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {

    }

    @Override
    public void delete(String id) {

    }

    @Override
    public List<StrategyCatalog> queryCatalogByDestId(String destId) {
        //攻略分类
        List<StrategyCatalog> list = strategyCatalogRepository.findByDestId(destId);

        //攻略分类下所有的攻略文章, limit 0 3
        for (StrategyCatalog catalog : list) {
            List<Strategy> sts = strategyService.getStrategyByCatalogId(catalog.getId());
            catalog.setStrategies(sts);
        }

        return list;
    }



    @Override
    public Object getVo() {
        Query query = new Query();
        List<String> destNames = mongoTemplate.findDistinct(query,"destName",StrategyCatalog.class,String.class);//查出所有城市
        List<CatalogVO> catalogs = new ArrayList<>();
        for (String destName:destNames){
            List<StrategyCatalog> strategyCatalogs = strategyCatalogRepository.findByDestName(destName);;//查出每个城市的分类列表

            CatalogVO catalogVO = new CatalogVO();
            catalogVO.setDestName(destName);//新建对象

            List<Map<String,Object>> list = new ArrayList<>();//完成对象


            for (int i=0 ;i<strategyCatalogs.size();i++){//上边的单个城市的分类对象
                Map<String,Object> map = new HashMap<>();
                map.put("id",strategyCatalogs.get(i).getId());
                map.put("name",strategyCatalogs.get(i).getName());
                list.add(map);
            }

            System.out.println(list);
            catalogVO.setMapList(list);
            catalogs.add(catalogVO);
        }
        System.out.println(catalogs);
        return catalogs;
    }


}
